package com.woniuxy.mqconsumer.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * description: RabbitmqConfiguration <br>
 * date: 2022/1/12 12:36 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * RabbitMq配置类，定义交换机，队列，绑定
 */
@Configuration
public class RabbitmqConfiguration {
    /**
     * 定义交换机
     */
    @Bean
    public TopicExchange exchangetopic() {
        return new TopicExchange("exchangetopic");
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue queueemail() {
        return new Queue("queueemail");
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue queuesms() {
        return new Queue("queuesms");
    }

    /**
     * 绑定队列到交换机
     * 同类型的bean有多个，参数注入
     * (@Qualifier("queueemail") Queue queue)
     * ( Queue queueemail)
     */

    @Bean
    public Binding bindingEmail(TopicExchange exchangetopic, Queue queueemail) {
        return BindingBuilder.bind(queueemail).to(exchangetopic).with("#.email.#");
    }
    //绑定队列到交换机中

    @Bean
    public Binding bindingSms(TopicExchange exchangetopic, Queue queuesms) {
        return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
    }

    /**
     * 死信
     * 1、定义一个正常队列，交换机，然后绑定队列交换机和路由
     * 2、定义一个死信的队列，交换机，然后绑定队列交换机和路由
     */
    //定义交换机
    @Bean
    public TopicExchange exchangeorder() {
        return new TopicExchange("exchangeorder");
    }

    //定义队列

    @Bean
    public Queue queueorder() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 10000);
        arguments.put("x-dead-letter-exchange", "exchangeorderdlx");
        arguments.put("x-dead-letter-routing-key", "orderdlx");
        Queue queueorder = new Queue("queueorder", true, false, false, arguments);
        return queueorder;
    }
    //绑定队列和交换机还有路由

    @Bean
    public Binding bindingqueueorder(Queue queueorder, TopicExchange exchangeorder) {
        return BindingBuilder.bind(queueorder).to(exchangeorder).with("#.order.#");
    }

    @Bean
    public TopicExchange exchangeorderdlx() {
        return new TopicExchange("exchangeorderdlx");
    }

    @Bean
    public Queue queueorderdlx() {
        Queue queueorderdlx = new Queue("queueorderdlx");
        return queueorderdlx;
    }

    @Bean
    public Binding bindingqueueorderdlx(Queue queueorderdlx, TopicExchange exchangeorderdlx) {
        return BindingBuilder.bind(queueorderdlx).to(exchangeorderdlx).with("#.orderdlx.#");
    }

    /**
     * 延迟队列
     */
    //延迟交换机
    @Bean
    public TopicExchange exchangedelay() {

        TopicExchange exchangedelay = new TopicExchange("exchangedelay");
        //交换机启动延迟
        exchangedelay.setDelayed(true);
        return exchangedelay;
    }

    //延迟队列

    @Bean
    public Queue queuedelay() {
        Queue queue = new Queue("queuedelay");
        return queue;
    }

    //绑定队列交换机和路由

    @Bean
    public Binding bindingqueuedelay(Queue queuedelay, TopicExchange exchangedelay) {

        return BindingBuilder.bind(queuedelay).to(exchangedelay).with("#.delay.#");
    }
}
